package com.project.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.project.Util.FullBean;
import com.project.Util.UtfUtil;
import com.project.bean.EmpBean;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lyl
 * description:员工servlet
 */
@Controller
public class EmpServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UtfUtil.utfUtil(req, resp);
        EmpBean employeeBean = new EmpBean();
        //调用工具类进行填充bean
        FullBean.fullEmpBean(employeeBean, req);
        resp.getWriter().println(employeeBean);
        //将java对象属性转换为json属性
        ObjectMapper objectMapper = new ObjectMapper();
        //提供新日期类型支持
        objectMapper.registerModule(new JavaTimeModule());
        String jsonStr = objectMapper.writeValueAsString(employeeBean);
        resp.getWriter().println(jsonStr);
    }

}
